$.picker.minDate = new Date(2014, 0, 1);
$.picker.maxDate = new Date(2020, 11, 31);
$.picker.value = new Date();
$.picker.addEventListener('change', function(e) {
	console.log("Selected date: " + e.value);
	//doSomething
});

$.email.addEventListener('change', function(e) {
	console.log('CHANGE!');
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

	if (reg.test(e.source.text) == false) {
		$.email._valid_flag = false;
	} else {
		$.email._valid_flag = true;
	}
});

function registar() {
	var userData = {
		utilizador : $.utilizador.value,
		password : Ti.Utils.md5HexDigest($.passwordField.value),
		email : $.email.value,
		primeiroNome : $.primeiroNome.value,
		ultimoNome : $.ultimoNome.value,
		//dataNascimento : new Date(e.data.birthday),
	};

	var xhr = Ti.Network.createHTTPClient({
		onload : function() {
			var json = this.responseText;
			var response = JSON.parse(json);
			if (response.autenticado == 1) {
				Ti.App.Properties.setObject('autenticado', userData);
				Alloy.Globals.login.close();
				closeWindow();
			} else {
				alert(response.mensagem);
			}
		},
		onerror : function(e) {
			Ti.API.debug(e.error);
			alert('error');
		},
		timeout : 100000 // in milliseconds
	});
	xhr.open('POST', ip + "/server/rest/servico-autenticacao/registar");
	xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
	//Define o encoding.
	xhr.send(JSON.stringify(userData));

}

function closeKeyboard(e) {
	e.source.blur();
}

function closeWindow() {
	$.registar.close();
}